<?php
import("ORG.Util.Function");
import("ORG.Util.Image");
import('ORG.Net.UploadFile');
import('@.Plugs.Exchange');
import('@.Org.BrandTool');

class BrandAction extends CommonAction {
		
	public function _initialize(){
		parent::_initialize();
		$param          =   $this->_get('_URL_');
		$controller     =   $param[0];
		$action         =   $param[1];
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	// 品牌列表
	public function brand_list(){		
		$brandList		=	M('Brand')->order("sort_order desc")->select();
		$this->assign("brandList",$brandList);	
		$this->display();
	}
	
	// 添加品牌
	public function add_brand(){
		$this->display();
	}
	
	// 处理添加品牌
	public function add_brand_ok(){		
		$BrandModel	=	new BrandModel();
		// 检查品牌名是否重复	    
	    $is_only 			= 	$BrandModel->is_only("brand_name", $_POST['brand_name']);
	    if (!$is_only){
	        $this->error($_POST['brand_name'] . "已经存在",U('brand/brand_list'));
	    }
	
	    // 对描述处理
	    if (!empty($_POST['brand_desc'])){
	        $_POST['brand_desc'] = $_POST['brand_desc'];
	    }
		// 构造数据组
	    $datas['is_show'] 		= 	isset($_REQUEST['is_show']) ? intval($_REQUEST['is_show']) : 0;	
		$datas['brand_desc'] 	= 	$_POST['brand_desc'];
		$datas['brand_name'] 	= 	$_POST['brand_name'];
		$datas['sort_order'] 	= 	$_POST['sort_order'];
		$datas['site_url'] 		= 	$_POST['site_url'];		
	    // 上传图片文件		
		$Upload                 =    new UploadFile();
		$Upload->maxSize        =    1000000; //大小为0.5M
		$Upload->allowExts      =    array('jpg', 'gif', 'png', 'jpeg');
		$ymd                    =    date('Ymd');
		$Upload->savePath       =    './Public/Upload/images/' . $ymd . '/'; //按日期目录上传文件
		$Upload->thumb          =    true;
		$Upload->thumbMaxWidth  =    '225';
		$Upload->thumbMaxHeight =    '125';				
		// 判断文件
		if($Upload->upload()){
			$fileInfoList       =    $Upload->getUploadFileInfo();
			$url				=	 $ymd . '/' . $fileInfoList[0]['savename'];
		}
		// logo图片地址	
		$datas['brand_logo']	=	 $url;
		// 入库
		if( $BrandModel->add($datas) ){
			$this->success("操作成功",U('brand/brand_list'));
		}
		else{
			$this->error("操作成功",U('brand/brand_list'));
		}		
	}
	
	// 修改品牌
	public function mod_brand(){
		
		$CommonModel	=	new CommonModel();
		
	    $sql 			= 	"SELECT brand_id, brand_name, site_url, brand_logo, brand_desc, brand_logo, is_show, sort_order " 
	    					. "FROM " . 'ybr_brand' . " WHERE brand_id='{$_REQUEST['id']}'";
	    $brand 			= 	$CommonModel->getRow($sql);

	    //t($brand);
	    $this->assign('brand',       $brand);
	    $this->assign('form_action', 'update');
	    $this->display();
	}
	
	public function mod_brand_ok(){
		
		$exc = new exchange("ybr_brand", null , 'brand_id', 'brand_name');
		
	    if ($_POST['brand_name'] != $_POST['old_brandname']){
	        /*检查品牌名是否相同*/
	        $is_only = $exc->is_only('brand_name', $_POST['brand_name'], $_POST['id']);
	
	        if (!$is_only){
	            sys_msg(sprintf($_LANG['brandname_exist'], stripslashes($_POST['brand_name'])), 1);
	        }
	    }
	
	    /*对描述处理*/
	    if (!empty($_POST['brand_desc'])){
	        $_POST['brand_desc'] = $_POST['brand_desc'];
	    }
	    /*显示*/	
	    $is_show = isset($_REQUEST['is_show']) ? intval($_REQUEST['is_show']) : 0;
	    /*URL*/
	    $site_url = sanitize_url( $_POST['site_url'] );	
		
	    $param = "brand_name = '$_POST[brand_name]',  site_url='$site_url', brand_desc='$_POST[brand_desc]', is_show='$is_show', sort_order='$_POST[sort_order]' ";
	    
	    /*图片*/		
		$Upload                 =    new UploadFile();
		$Upload->maxSize        =    1000000; //大小为1M
		$Upload->allowExts      =    array('jpg', 'gif', 'png', 'jpeg');
		$ymd                    =    date('Ymd');
		$Upload->savePath       =    './Public/Upload/images/' . $ymd . '/'; //按日期目录上传文件
		$Upload->thumb          =    true;
		$Upload->thumbMaxWidth  =    '225';
		$Upload->thumbMaxHeight =    '125';				
		/*获取上传文件信息*/
		if($Upload->upload()){
			$fileInfoList       =    $Upload->getUploadFileInfo();
			$url				=	 $ymd . '/' . $fileInfoList[0]['savename'];
		}
		else{
			//t($Upload->getErrorMsg());
		}
		/*文件不为空,记录地址*/
	    if(!empty($url)){
			$param .= " ,brand_logo = '{$url}' ";
	    }
		
	    if ($exc->edit($param,  $_POST['id'])){	    		
	    	/*记录操作*/
	        admin_log($_POST['brand_name'], 'edit', 'brand');
	        $this->success("操作成功",U('brand/brand_list'));
	    }
	    else{
	        $this->error("操作失败");
	    }		
	}
	
	// 删除品牌
	public function del_brand(){
		$brand_id		=	intval( $_GET['id'] );
	    // 删除该品牌的图标
		$brandInfo		=	M('Brand')->field("brand_logo")->where("brand_id = '{$brand_id}'")->find();
		if(!empty($brandInfo['brand_logo'])){
			@unlink("./Public/Upload/images" . $brandInfo['brand_logo']);
		}
		M("Brand")->where("brand_id = '{$brand_id}'")->delete();
	    // 更新商品的品牌编号	   
	    M('Goods')->where("brand_id = '{$brand_id}'")->setField("brand_id",0);
	    $this->success("操作成功",U('brand/brand_list'));
	    //$this->redirect($url, $params)("操作成功",U('brand/brand_list'));
	}
				
}